package com.zkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.zkj.constant.MessageConstant;
import com.zkj.context.BaseContext;
import com.zkj.dto.ShoppingCartDTO;
import com.zkj.entity.Dish;
import com.zkj.entity.Setmeal;
import com.zkj.entity.ShoppingCart;
import com.zkj.exception.AccountNotFountException;
import com.zkj.mapper.DishMapper;
import com.zkj.mapper.SetmealMapper;
import com.zkj.mapper.ShoppingCartMapper;
import com.zkj.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author zkj
 * @since 2024-08-22
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        /**
         * 新增的时候需要考虑
         * 1. 如果存在 购物车中已经存在的商品，则数量+1
         *   1. 查询套餐ID和用户ID对应的购物车数据，如果数量大于零
         *   2. 查询菜品ID和口味和用户ID对应的购物车数据，如果数量大于零
         * 2. 如果不存在 则新增一条记录
         */
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        log.info("BaseContext.getCurrentId() {}", BaseContext.getCurrentId());
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.existShoppingCartList(shoppingCart);
        if (shoppingCarts != null && !shoppingCarts.isEmpty()) {
            shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart);
        } else {
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart =
                        ShoppingCart.builder().name(dish.getName()).image(dish.getImage()).amount(dish.getPrice()).dishId(dish.getId()).build();
            } else {
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart =
                        ShoppingCart.builder().name(setmeal.getName()).image(setmeal.getImage()).amount(setmeal.getPrice()).setmealId(setmeal.getId()).build();
            }
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> shoppingCartList() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (currentId != null) {
            queryWrapper.eq(ShoppingCart::getUserId, currentId);
            return shoppingCartMapper.selectList(queryWrapper);
        }
        return new ArrayList<ShoppingCart>();
    }

    public void cleanShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (currentId != null) {
            queryWrapper.eq(ShoppingCart::getUserId, currentId);
            shoppingCartMapper.delete(queryWrapper);
        }
    }
}
